package com.iwuke.dance.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

/**
 * @author: 魏从响
 * @date: 2025/9/18
 */
public class GlideUtil {

    //清除内存缓存： 必须在UI线程中调用
    //Glide.get(CYCMerchantsApplication.getContext()).clearMemory();
    // 清除磁盘缓存： 必须在后台线程中调用，建议同时clearMemory()
    //Glide.get(CYCMerchantsApplication.getContext()).clearDiskCache();
    //.diskCacheStrategy(DiskCacheStrategy.NONE)  //禁止磁盘缓存：

    public static void setGif(Context context, Object resourceId
            , ImageView imageView){
        if (context==null||((Activity)context).isFinishing()){
            return ;
        }

//        Glide.with(context).load(resourceId).asGif()
//                .diskCacheStrategy(strategy).into(imageView);

        RequestOptions options = new RequestOptions().transform(new CornersTransform(Util.px2dip(context,0)));

        Glide.with(context)
                .load(resourceId)
                .apply(options)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        // 根据图片的实际长宽判断，如果宽大于长则拉伸，如果宽小于等于长则居中显示
                        int width = resource.getIntrinsicWidth();
                        int height = resource.getIntrinsicHeight();
                        if (width > height) {//FIT_XY配合XY  FIT_CENTER安装中心  CENTER_INSIDE内部居中
                            ((ImageView) imageView).setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                        } else if (width <= height) {
                            ((ImageView) imageView).setScaleType(ImageView.ScaleType.FIT_XY);
                        }
                        return false;
                    }
                })
                .into((ImageView) imageView);


    }

    public static void setImageView(Context context, Object resourceId,ImageView imageView) {
        if (((Activity)context).isFinishing()){
            return;
        }
        Glide.with(context).load(resourceId).into(imageView);
    }

    public static void setImageView2(Context context, Object resourceId,ImageView imageView) {//.thumbnail(0.5f)
        if (((Activity)context).isFinishing()){
            return;
        }
        Glide.with(context).load(resourceId).into(imageView);
        //Glide.with(context).load(resourceId).override(Util.dip2px(context,140), Util.dip2px(context,140)).into(imageView);
    }

    public static void setImageViewRadius(Context context, Object resourceId,int value,ImageView imageView) {
        Glide.with(context).load(resourceId).apply(RequestOptions
                        .bitmapTransform(new RoundedCorners(value)))
                .into(imageView);
    }

    public static void setImageViewWH(Context context, Object resourceId,ImageView imageView,int radius) {
        if (((Activity)context).isFinishing()){
            return;
        }
//        RequestOptions options = new RequestOptions().transform(new CornersTransform(Util.px2dip(context,radius)));
//
//        Glide.with(context)
//                .load(resourceId)
//                .apply(options)
//                .listener(new RequestListener<Drawable>() {
//                    @Override
//                    public boolean onLoadFailed(GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
//                        return false;
//                    }
//
//                    @Override
//                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                        // 根据图片的实际长宽判断，如果宽大于长则拉伸，如果宽小于等于长则居中显示
//                        int width = resource.getIntrinsicWidth();
//                        int height = resource.getIntrinsicHeight();
//                        if (width > height) {//FIT_XY配合XY  FIT_CENTER安装中心  CENTER_INSIDE内部居中
//                            ((ImageView) imageView).setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//                        } else if (width <= height) {
//                            ((ImageView) imageView).setScaleType(ImageView.ScaleType.FIT_XY);
//                        }
//                        return false;
//                    }
//                })
//                .into((ImageView) imageView);
    }

}

